package com.bosssoft.controller;


import com.aliyun.oss.model.OSSObject;
import com.bosssoft.enums.OssCode;
import com.bosssoft.response.Result;
import com.bosssoft.service.AliOssService;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Aria
 */
@Api(tags = "图片存储")
@RequestMapping("/alioss")
@RestController
public class AliOssController {


    @Resource
    private AliOssService aliOssService;

    @ApiOperation("上传文件")
    @ApiModelProperty(value = "上传文件")
    @PostMapping("/uploadImgFile")
    public Result upload(MultipartFile file) {
        String url = aliOssService.upload(file);
        return Result.ok().data("url", url);
    }


    @ApiOperation("获取字节流")
    @ApiModelProperty(value = "获取字节流")
    @PostMapping("/getByets")
    public Result getByets(@RequestBody String url) {
        byte[] bytes = aliOssService.getBytes(url);
        String s = bytes.toString();

        return Result.ok().data("bytes", bytes);
    }

    @ApiOperation("删除文件")
    @ApiModelProperty(value = "删除文件")
    @PostMapping("/deleteImgFile")
    public Result delete(String fileUrl) {
        if (fileUrl == null) {
            return Result.error(OssCode.IMAGE_NOT_FOUND_ERROR);
        }
        try {
            fileUrl = fileUrl.split(".com.")[1];
            aliOssService.deleteFile(fileUrl);
        } catch (Exception e) {
        }
        return Result.ok();
    }
}
